package cn.bdqn.web;

import cn.bdqn.entity.District;
import cn.bdqn.service.AirService;
import cn.bdqn.service.DistrictService;
import cn.bdqn.service.impl.AirServiceImpl;
import cn.bdqn.service.impl.DistrictServiceImpl;
import cn.bdqn.util.PageBean;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


@WebServlet(value = "/index")
public class IndexServlet extends HttpServlet {
    /*
    1. 首页.
    路径:
        GET   	http://localhost/index
    参数:
        districtId = integer(非必传)
        currentPage = integer(默认为1)
        pageSize = integer(默认为5)
    响应:
        1. 全部区域数据,放到request域中.
        2. pageBean数据,放到request域中.
        3. 转发到/WEB-INF/index.jsp
     */

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1. 接收参数.
        String districtIdStr = req.getParameter("districtId");
        String currentPageStr = req.getParameter("currentPage");
        String pageSizeStr = req.getParameter("pageSize");
        //2. 封装参数.
        Integer districtId = null;
        if(districtIdStr != null&&districtIdStr!=""){
            districtId = Integer.valueOf(districtIdStr);
            //区域ID是前端页面给后台的查询条件
            //为了页面回显查询条件将districtid放到req域中
            req.setAttribute("districtId",districtId);
        }

        Integer currentPage = 1;
        if(currentPageStr != null){
            currentPage = Integer.valueOf(currentPageStr);
        }
        Integer pageSize = 5;
        if(pageSizeStr != null){
            pageSize = Integer.valueOf(pageSizeStr);
        }
        //3. 调用service层方法,查询指定数据
        AirService airService = new AirServiceImpl();
        DistrictService districtService = new DistrictServiceImpl();
        //3.1. 全部区域数据
        List<District> districtList = districtService.findAll();
        req.setAttribute("districtList",districtList);
        //3.2. pageBean数据
        PageBean pageBean = airService.findAirByConditionAndByPage(currentPage, pageSize, districtId);
        req.setAttribute("pageBean",pageBean);
        //4. 转发回/WEB-INF/index.jsp
        req.getRequestDispatcher("/WEB-INF/index.jsp").forward(req,resp);
    }
}
